﻿var gAllText = "=====请选择=====";

//下拉option对象定义
function selectOption(id,parentId,display,value) 
{
	this.id       = id;
	this.parentId = parentId;
	this.display  = display;
	this.value    = value;
}

//产品分类定义

var select_catalogs = new Array();
var select_i = 0 ;



//得到子下拉框的option项
function selectGetChildOptions(options,parentId)
{
	
	var i ;
	var child = new Array();
	var count = 0 ;
	for (i=0;i<options.length;i++)
	{
		var option = options[i];
		if(option.parentId==parentId)
		{
			child[count++] = option ;
		}
	}
	return child ;
}

//在页面加入下拉框, 
//name           = 下拉框名称
//nextSelectName = 下级下拉框名称
//parentId       = 下拉框的parentId
//defValue       = 当前值
function selectAdd(name,nextSelectName,parentId,defValue,callfunc)
{
	
	if(parentId=="") parentId=-9999;
	
	var child = selectGetChildOptions(select_catalogs,parentId);
	
	var s = "";
	
	s += "<select nextSelectName='"+nextSelectName+"' name='"+name+"' callfunc='"+callfunc+"' onchange='selectOnChange(\""+name+"\");'>";
	
	s += "<option value=''>"+gAllText+"</option>" ;
	
	for(var i=0;i<child.length;i++)
	{	
		var option = child[i] ;
		var sSelect = "" ;
		if(option.value == defValue)
		{
			sSelect = " selected " ;
		}
		s += "<option "+ sSelect+ " id= "+ option.id +" value='"+option.value+"'>"+option.display+"</option>";
	}
	
	s += "</select>" ;
	
	document.write(s);
}


//删除掉下拉框的所有option
function selectDelete(nameSel)
{
	var objs = document.all(nameSel);
	for(var i=objs.length-1;i>=0;i--)
	{
		var obj = objs.item(i);
	    document.all(nameSel).options.remove(i);
	}
}

//得到下拉框的选中值
function selectGetValue(n)
{
	var sel = document.all(n);
	var opts = sel.options;
	for(var i=0;i<opts.length;i++)
	{
		var opt = opts.item(i);
		if(opt.selected)
		{
			return opt.value;
		}
	}
	return "ERROR";
}   

function selectGetOption(n)
{
	var sel = document.all(n);
	var opts = sel.options;
	for(var i=0;i<opts.length;i++)
	{
		var opt = opts.item(i);
		if(opt.selected)
		{
			return opt;
		}
	}
	return null;
}   

//下拉框选择变动时间处理函数
function selectOnChange(selectName)
{
		
	var select = document.all(selectName);
	
	var nextSelectName = select.nextSelectName ;
	
	if(nextSelectName=="" || nextSelectName == "undefined" || nextSelectName == undefined) return ;
	
	selectDelete(nextSelectName);
	
	var v = selectGetValue(selectName) ;
	
	if(v=="") v=-9999;
	
	var child = selectGetChildOptions(select_catalogs,v);
	
	var allOption = document.createElement("OPTION");
	allOption.text  = ""+gAllText+"";
	allOption.value = "";
	
	document.all(nextSelectName).options.add(allOption);
		
	for(var i=0;i<child.length;i++)
	{
	    var option = child[i] ;
		var oOption = document.createElement("OPTION");
		oOption.text  = option.display;
		oOption.value = option.value;
		document.all(nextSelectName).options.add(oOption);
	}
	
	selectOnChange(nextSelectName);
}


//-----
var select_options = new Array();

function selectCommonAddOptions(options,i)
{
	select_options[i] = options ;
}

function selectCommonGetOptions(i)
{
	return select_options[i] ;
}

//下拉框选择变动时间处理函数
function selectCommonOnChange(selectName)
{
	var select = document.all(selectName);
	
	var nextSelectName = select.nextSelectName ;
	
	//没有要处理的下级下拉框
	if(nextSelectName==null||nextSelectName=="undefined"||nextSelectName=="") 
	{
		if(select.callfunc==null || select.callfunc=="" || select.callfunc=="undefined") return ;
	
		eval(select.callfunc);
		
		return ;
	}
	
	
	selectDelete(nextSelectName);
	
	var v = selectGetValue(selectName) ;
	
	//if(v=="") v=-9999;
	
	var optionsIndex = select.optionsIndex ;
	
	var opt = selectGetOption(selectName);	
	
	var child;
	
	
	if(null==opt||opt.id==null||opt.id==""||opt.id=="undefined")
	{
		child = new Array();
	}
	else
	{
		child = selectGetChildOptions(selectCommonGetOptions(optionsIndex),opt.id);
	}
	
	if(child.length==0 || child.length>1 || child.length==1)
	{
		var allOption = document.createElement("OPTION");
		allOption.text  = ""+gAllText+"";
		allOption.value = "";
		document.all(nextSelectName).add(allOption);
	}	
	for(var i=0;i<child.length;i++)
	{
	    var option = child[i] ;
		var oOption = document.createElement("OPTION");
		oOption.text  = option.display;
		oOption.value = option.value;
		oOption.id    = option.id ;
		document.all(nextSelectName).add(oOption);
	}
	
	
	if(select.callfunc!=null || select.callfunc!="" || select.callfunc!="undefined") 
	{
		eval(select.callfunc);
	}

	selectCommonOnChange(nextSelectName);
	
}


//在页面加入下拉框, 
//name           = 下拉框名称
//nextSelectName = 下级下拉框名称
//parentId       = 下拉框的parentId
//defValue       = 当前值
//optionsIndex   = options数据的索引
function selectCommonAdd(name,nextSelectName,parentId,defValue,optionsIndex,callfunc)
{
	
	//if(parentId=="") parentId=-9999;
	
	var child = selectGetChildOptions(selectCommonGetOptions(optionsIndex),parentId);
	
	var s = "";
	
	s += "<select optionsIndex="+optionsIndex+" callfunc="+callfunc+" nextSelectName='"+nextSelectName+"' name='"+name+"' onchange='selectCommonOnChange(\""+name+"\");'>";
	
	if(child.length>1 || child.length==0 || child.length==1)
	{
		s += "<option value=''>"+gAllText+"</option>" ;
	}
	for(var i=0;i<child.length;i++)
	{	
		var option = child[i] ;
		var sSelect = "" ;
		if(option.value == defValue)
		{
			sSelect = " selected " ;
		}
		s += "<option "+ sSelect+ " id= "+ option.id +" value='"+option.value+"'>"+option.display+"</option>";
	}
	
	s += "</select>" ;
	
	document.write(s);
}